﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using ChessServises;
using System.IO;
namespace WpfChessClient.Model
{
    public class ChessModel:INotifyPropertyChanged
    {
        private const string CHESS_SERVICE_BASE_ADDRESS = "http://localhost:3326/ChessService.svc/";
        private Guid userId;
        private int currentTableId;
        private ChessBoard gameBoard;
        private DateTime lastTableVersion;
        public event PropertyChangedEventHandler PropertyChanged;

        public ChessModel()
        {
          
        }

        public int TableId
        {
            get
            {
                return currentTableId;
            }
            set
            {
                currentTableId = value;
            }
        }

        public string UserName
        {
            get;
            set;
        }

        public Guid UserId
        {
            get
            {
                if (userId == null)
                {
                    GetNewUserId();
                }

                return userId;
            }
        }

        public List<int> GameList
        {
            get
            {
                return GetTableIds();
            }
        }


        public int CreateGame()
        {
            using (HttpClient client = new HttpClient(CHESS_SERVICE_BASE_ADDRESS))
            {
                string uri = "NewGame";

                HttpContent content = HttpContentExtensions.CreateJsonDataContract<Guid>(userId);
                using (HttpResponseMessage responce = client.Post(uri, content))
                {
                    responce.EnsureStatusIsSuccessful();
                    int tableId = responce.Content.ReadAsDataContract<int>();
                    currentTableId = tableId;
                    return tableId;
                }
            }
        }

        private List<int> GetTableIds()
        {
            using (HttpClient client = new HttpClient(CHESS_SERVICE_BASE_ADDRESS))
            {
                using (HttpResponseMessage responce = client.Get("Games"))
                {
                    responce.EnsureStatusIsSuccessful();
                    List<int> ids = responce.Content.ReadAsDataContract<List<int>>();

                    return ids;
                }
            }
        }     

        private void GetNewUserId()
        {
            using (HttpClient client = new HttpClient(CHESS_SERVICE_BASE_ADDRESS))
            {
                string uri = "NewUser/" + Uri.EscapeUriString(UserName);
                using (HttpResponseMessage responce = client.Get(uri))
                {
                    responce.EnsureStatusIsSuccessful();
                    userId = responce.Content.ReadAsDataContract<Guid>();                    
                }
            }
        }

        private void GetLatestGameBoard(int tableId, DateTime lastVersion)
        {
            using (HttpClient client = new HttpClient(CHESS_SERVICE_BASE_ADDRESS))
            {
                GetTableParameters parameters = new GetTableParameters()
                {
                    TableId = tableId,
                    LastVersion = lastVersion
                };
                string uri="GetGame";
                HttpContent content = HttpContentExtensions.CreateJsonDataContract<GetTableParameters>(parameters);
                using (HttpResponseMessage responce = client.Post(uri, content))
                {
                    responce.EnsureStatusIsSuccessful();
                    using (Stream boardStream = responce.Content.ReadAsStream())
                    {
                        BinaryFormatter formatter = new BinaryFormatter();
                        ChessBoard board = formatter.Deserialize(boardStream) as ChessBoard;
                    }
                }

            }
        }

        private void MakeChessMove(ChessBoard board)
        {
            
        }
    }
}
